<?php
// +----------------------------------------------------------------------
// | Etion [ http://www.etion.cn ]
// +----------------------------------------------------------------------
// | File: BaseAction.class.php
// +----------------------------------------------------------------------
// | Date: Wed Apr 21 13:44:16 CST 2010
// +----------------------------------------------------------------------
// | Author: 李汝康 <37739419@qq.com>
// +----------------------------------------------------------------------
// | 文件描述: 前台活动报名页面
// +----------------------------------------------------------------------

/**
 * @name 前台活动报名页面
 *
 */
class FformAction extends FbaseAction {

	/**
	 * activity 表对象模型实例
	 * @var object
	 */
	protected $_formorder = '';

	/**
	 * 栏目数据
	 * @var array
	 */
	private $_category_data = array();

	/**
	 * 活动详细信息
	 * @var array
	 */
	protected $_data = array();

	/**
	 * 执行的动作
	 * @var string
	 */
	protected $_action = '';

	/**
	 * @name初始化
	 */
	protected function _initialize() {
		parent::_initialize();
		$this->$_formorder = D ('Form','admin');
		$in = &$this->in;
		if (CATID) $catid = CATID;

		elseif ($in['catid']) $catid = intval($in['catid']);
		else {
			header("Content-type: text/html; charset=utf-8");
			die('<script>alert("缺少参数catid！"); location.href="' . $this->forward . '";</script>');
		}
		$in['catid'] = $catid;

		$in['_tablename'] = 'formorder';

		$this->_category_data = F ('category_'.$catid);

		$this->_request_file = substr(REQUEST_FILE,0,strlen(REQUEST_FILE)-strlen(C('URL_HTML_SUFFIX')));

		if (!$this->_category_data) parent::h404();
		$this->assign('cat', $this->_category_data);	//栏目信息

	}

	/**
	 * @name分发操作
	 */
	protected function _empty() {
		$baseurl = str_replace('?', '/', $this->_urls['baseurl']);
		$method = explode('/', $baseurl);
		$method = $method[0];
		if (method_exists($this, $method)) {
			call_user_func(array($this, $method));
		} else {
			$this->h404();
		}
	}


	/**
	 * @name活动报名频道页
	 *
	 */
	public function index() {
		$in = &$this->in;
		//seo设置
		$seo['seotitle'] = $this->_category_data['seotitle'] ? $this->_category_data['seotitle'] : $this->_category_data['name'];
		$seo['seokeywords'] = $this->_category_data['seokeywords'] ? $this->_category_data['seokeywords'] : $this->_category_data['name'];
		$seo['seodescription'] = $this->_category_data['seodescription'] ? $this->_category_data['seodescription'] : $this->_category_data['description'];
		//指定当前页面唯一链接
		if ($this->_page > 1) {
			$seo['url'] = C ('SITEURL') . $this->_category_data['url'] . 'index_' . $this->_page . C('URL_HTML_SUFFIX');
		} else {
			$seo['url'] = C ('SITEURL') . $this->_category_data['url'];
		}
		//字符替换
		$seo = parent::meta_replace($seo);
		$this->assign('seo',$seo); //meta信息
		$this->assign('cat',$this->_category_data);	//栏目信息
		$this->assign('p',$this->_page); //当前页码
		$this->display($this->_category_data['setting']['template']['index']);
	}

	/*定义常规的crud操作、如果是对某张表的数据进行普通的crud操作，则不需要写代码，系统会自动调用下面定义的几个方法*/

	/**
	 * 常规的添加、更新操作、如果此方法不适用，则可以在对应的action中override
	 *
	 * 支持ajax提交方式和普通表单提交方式
	 */
	public function add() {
		$in = &$this->in;
		if ($in ['dosubmit']) {
			if (! $in ['_tablename'])
			$this->message ( '没有指定操作表！' );
			$name = $in ['_tablename']; //数据表名
			//		die($this->getInTableName($name));

			$_m = M ( 'formorder'); //实例化表模型类
			$_keyid = $_m->getPk ();
			//用create()创建数据对象，以可以使用系统内置的数据自动验证功能以及令牌验证功能
			$in['info'][C ('TOKEN_NAME')] = $in[C ('TOKEN_NAME')];
			//判断文件上传，处理文件上传
			if (!empty($_FILES['filename']['name']) && !$in['info']['filename']) {
				//处理简历上传
				import("ORG.Net.UploadFile");
				$upload = get_instance_of ( 'UploadFile' ); // 实例化上传类
				$upload->maxSize  = intval($this->_category_data['setting']['maxsize'])
				? 1024*$this->_category_data['setting']['maxsize']
				: 307200; // 设置附件上传大小
				$upload->allowExts  = (empty($this->_category_data['setting']['allow_ext'])
				? array('jpg','gif')
				: explode('|',$this->_category_data['setting']['allow_ext'])); // 设置附件上传类型
				$upload->savePath =  C ( 'UPLOAD_DIR').'files/'; // 设置附件上传目录
				$upload->saveRule = C ( 'UPLOAD_FILE_RULE' );
				if(!$upload->upload()) { // 上传错误提示错误信息
					$msg .= $upload->getErrorMsg();
				}else{ // 上传成功获取上传文件信息
					$fileinfo =  $upload->getUploadFileInfo();
					$in['info']['upload_name'] = $fileinfo [0]['name'];
					$in['info']['filename'] = 'files/' . $fileinfo [0]['savename'];
				}
			}
			$in['info']['content'] = implode("|||", $in ['info']);


			if ( $_m->create ( $in ['info'] ) ) {
					
				if ( isset( $in['info']['verify']) ){
					if ($_SESSION['verify'] != md5($in['info']['verify'])) $this->message('验证码输入错误！');
				}
				if (! empty ( $in ['info'] [$_keyid] )) { //更新
					$keyid = $_m->save ();
				} else { //添加

					$keyid = $_m->add ();
					if ($keyid) $in['info'][$_keyid] = $keyid;
				}
				if (false !== $keyid) { //添加数据
					//dump($in ['info']);
					if (method_exists ( $_m, 'cache' )) { //调用缓存处理;
						$_m->cache ( ($in['info'][$_keyid] ? $in['info'][$_keyid] : $keyid), $in ['info'] );
					}
					//返回处理信息
					if ($in ['ajax'])
					$this->ajaxReturn ( $in ['info'], '记录保存成功！', 1, 'json' );
					else
					$this->message ( '记录保存成功！' );
				} else {
					//返回处理信息
					if ($in ['ajax'])
					$this->ajaxReturn ( '', $_m->getError () . '<br />数据保存失败！', 1, 'json' );
					else
					$this->message ( $_m->getError () . '<br />数据保存失败！' );
				}
			} else {
				if ($in ['ajax'])
				$this->ajaxReturn ( '', $_m->getError (), 1, 'json' );
				else
				$this->message ( $_m->getError ().'记录保存失败！' );
			}
		}
		//获取数据
		if (!empty($in['_tablename'])) {
			$name = $in ['_tablename']; //数据表名
			$_m = M ('formorder' ); //实例化表模型类
			$_keyid = $_m->getPk ();
			if ( $in [$_keyid] ) { //编辑
				$keyid = $in [$_keyid] ;
				$data = $_m->find ( $keyid );
				if (isset($data['parentid']) && $data['parentid']>0) {
					$this->assign('parent_data',$_m->find($data['parentid']));
				}
				$this->assign ( 'data', $data );
			}
		}

		$this->display($this->_category_data['setting']['template']['show']);

	}



}